//
//  LandViewController.swift
//  liaoningdangdai
//
//  Created by 白雨桐 on 2019/11/27.
//  Copyright © 2019 白雨桐. All rights reserved.
//

import UIKit
import SwiftyJSON
import SVProgressHUD


//登陆类
class LandViewController: BaseViewController {


    // 绑定标签
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var loginBtn: UIButton!
    @IBOutlet weak var backBtn: UIButton!
    

    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        // 执行事件
        setupView()
    }
}

// MARK: - 监听按钮
extension LandViewController {
        
    @objc func setupView() {
        // 监听登陆按钮
        loginBtn.addTarget(self, action: Selector(("loginBtnClick")), for: .touchUpInside)
        // 返回上一页按钮
        backBtn.addTarget(self, action: Selector(("backBtnClick")), for: .touchUpInside)
    }
}

//MARK: - 绑定监听事件
extension LandViewController {
    
    // 绑定登陆按钮
    @objc private func loginBtnClick()
    {
        loadUser_login()
    }
    
    // 返回上一页
    @objc private func backBtnClick()
    {
        self.dismiss(animated: true, completion: nil)
    }
    
    
}

// MARK: - 请求登陆接口
extension LandViewController
{
    
    func loadUser_login()
    {
        
        // 获取绑定标签
        let txtUsername = username?.text
        let txtPassword = password?.text
        let arr1 = [txtUsername,txtPassword,"","","phone","",""]
        
        NetworkTools.share.user_login(code: arr1 as Array<Any>) { (result, error) in
            
            // 错误校验
            if error != nil
            {
                print(error!)
                return
            }
            
            // 拿到结果
            guard result != nil else {
                print("登陆失败！！！")
                return
            }
            
            // 处理请求成功的数据
            //let json = JSON(accountDict)
            
            // 字典转模型
            
            //let account = User(dict: json["REP"]["RESULT"])
            
            //print(json)
            // 闭包传参需要self
            self.loadInfo(json: result!)
            
        }
    }
    
    
    private func loadInfo(json : [String : AnyObject])
    {
        // 处理请求成功的数据
            let json = JSON(json)
            // 如果接口请求成功但是登陆失败U是空的
            let strUser : String? = json["REP"]["RESULT"]["U"].stringValue
            let strRetcode : String? = json["REP"]["RESULT"]["RETCODE"].stringValue
            
            // 搞个对象
            let user = User()
            user.U = strUser
            user.RETCODE = strRetcode
            
        
            if (strUser == nil) {
                print("用户名不能为空")
                return
            }
        
            if (strRetcode == nil) {
                print("密码不能为空")
                return
            }
            else if ( strUser == "" || strUser == nil )
            {
                print("登陆失败")
                return
            } else {
                print("登陆成功")
                // 归档，元素：成功状态，用户名，密码
                self.saveWithFile(a : 1, u: strUser!, retcode: strRetcode!)
                // 成功跳转
                self.goBack(identifier: "Allproducts")
            }
    }
    
    
}


extension LandViewController {
    
    
}


